import { Inject, Provide } from '@midwayjs/core'
import { Context } from '@midwayjs/koa'

@Provide()
export class IpService {
  @Inject()
  ctx: Context

  /** 默认 IP */
  DEFAULT_IP = 'XX.XX'

  getByCtx() {
    if (this.ctx.req === null) {
      return this.DEFAULT_IP
    }

    const ip = this.ctx.ip

    return ip.includes('127.0.0.1') ? '127.0.0.1' : ip
  }
}
